using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;
using System.Data.OleDb;
using System.Web;

namespace AliCMS.SystemModule.Bll
{
    public partial class Channel : DbObjectBase, IChannel
    {
        public class ExtraInstance : DbObjectBase, SystemModule.Channel.IExtraInstance
        {
			public ExtraInstance(int instanceID, string lang, bool checkRight, string database)
				: base(instanceID, lang, checkRight, database)
			{
			}

			public override object ExecuteService(HttpContext context)
			{
                object result = null;

                string operation = context.Request.QueryString["Operation"];
                if (operation == "Add")
                {
                    Dictionary<string, object> item = new Dictionary<string, object>();
                    string value = null;
                    item[Alias.Channel.ExtraInstance.ChannelID] = context.Request.Form["ChannelID"];
                    value = context.Request.Form["BindingInstanceID"];
                    if (value != null) item[Alias.Channel.ExtraInstance.BindingInstanceID] = value;
                    value = context.Request.Form["BindingChannelID"];
                    if (value != null) item[Alias.Channel.ExtraInstance.BindingChannelID] = value;
                    item[Alias.Channel.ExtraInstance.Mark] = context.Request.Form["Mark"];

                    result = Add(item);
                }
                else if (operation == "Modify")
                {
                    int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                    Dictionary<string, object> changes = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.Form["ChannelID"];
                    if (value != null) changes[Alias.Channel.ExtraInstance.ChannelID] = value;
                    value = context.Request.Form["BindingInstanceID"];
                    if (value != null) changes[Alias.Channel.ExtraInstance.BindingInstanceID] = value;
                    value = context.Request.Form["BindingChannelID"];
                    if (value != null) changes[Alias.Channel.ExtraInstance.BindingChannelID] = value;
                    value = context.Request.Form["Mark"];
                    if (value != null) changes[Alias.Channel.ExtraInstance.Mark] = value;

                    Modify(id, changes);
                }
                else if (operation == "Delete")
                {
                    int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                    Delete(id);
                }
                else if (operation == "GetInnerIDFromGuid")
                {
                    Guid guid = new Guid(context.Request.QueryString["ID"]);
                    result = GetInnerIDFromGuid(guid);
                }
                else if (operation == "List")
                {
                    string value = null;

                    int page = -1;
                    value = context.Request.QueryString["Page"];
                    if (value != null) page = Convert.ToInt32(value);

                    int pageSize = -1;
                    value = context.Request.QueryString["PageSize"];
                    if (value != null) pageSize = Convert.ToInt32(value);

                    string condition = context.Request.QueryString["Condition"];
                    string sort = context.Request.QueryString["Sort"];

                    result = List(page, pageSize, condition, sort);
                }
                else
                {
                    throw new Exception("lang_header_param");
                }

                return result;
            }

			public int GetInnerIDFromGuid(Guid guid)
            {
                return Dal.GetInnerIDFromGuid(guid);
            }
			public Guid GetGuidFromInnerID(object id)
            {
                return Dal.GetGuidFromInnerID(id);
            }
			public Dictionary<string, object> Get(object id)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Get", guid);
                }

                return Dal.Get(id);
            }
			public Guid Add(Dictionary<string, object> item)
            {
                if (CheckRight)
                {
                    ValidateRight("Add", Guid.Empty);
                }

                if (!item.ContainsKey(Alias.Channel.ExtraInstance.ID) || item[Alias.Channel.ExtraInstance.ID].ToString() == String.Empty)
                    item[Alias.Channel.ExtraInstance.ID] = Guid.NewGuid();
                else if (!(item[Alias.Channel.ExtraInstance.ID] is Guid))
                    item[Alias.Channel.ExtraInstance.ID] = new Guid(item[Alias.Channel.ExtraInstance.ID].ToString());

                if (!(item[Alias.Channel.ExtraInstance.ChannelID] is Guid))
                    item[Alias.Channel.ExtraInstance.ChannelID] = new Guid(item[Alias.Channel.ExtraInstance.ChannelID].ToString());

                if (item.ContainsKey(Alias.Channel.ExtraInstance.BindingInstanceID))
                {
                    string bindingInstanceID = Convert.ToString(item[Alias.Channel.ExtraInstance.BindingInstanceID]);
                    if (String.IsNullOrEmpty(bindingInstanceID))
                        item[Alias.Channel.ExtraInstance.BindingInstanceID] = DBNull.Value;
                    else
                        item[Alias.Channel.ExtraInstance.BindingInstanceID] = new Guid(bindingInstanceID);
                }

                if (item.ContainsKey(Alias.Channel.ExtraInstance.BindingChannelID))
                {
                    string bindingChannelID = Convert.ToString(item[Alias.Channel.ExtraInstance.BindingChannelID]);
                    if (String.IsNullOrEmpty(bindingChannelID))
                        item[Alias.Channel.ExtraInstance.BindingChannelID] = DBNull.Value;
                    else
                        item[Alias.Channel.ExtraInstance.BindingChannelID] = new Guid(bindingChannelID);
                }

                Dal.Add(item);

                Dal.EndWork();
                Manager.UpdateExtraInstances();

                return (Guid)item[Alias.Channel.ExtraInstance.ID];
            }
			public void Modify(int id, Dictionary<string, object> changes)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Modify", guid);
                }

                if (changes.ContainsKey(Alias.Channel.ExtraInstance.ChannelID) && !(changes[Alias.Channel.ExtraInstance.ChannelID] is Guid))
                    changes[Alias.Channel.ExtraInstance.ChannelID] = new Guid(changes[Alias.Channel.ExtraInstance.ChannelID].ToString());

                if (changes.ContainsKey(Alias.Channel.ExtraInstance.BindingInstanceID))
                {
                    string bindingInstanceID = Convert.ToString(changes[Alias.Channel.ExtraInstance.BindingInstanceID]);
                    if (String.IsNullOrEmpty(bindingInstanceID))
                        changes[Alias.Channel.ExtraInstance.BindingInstanceID] = DBNull.Value;
                    else
                        changes[Alias.Channel.ExtraInstance.BindingInstanceID] = new Guid(bindingInstanceID);
                }

                if (changes.ContainsKey(Alias.Channel.ExtraInstance.BindingChannelID))
                {
                    string bindingChannelID = Convert.ToString(changes[Alias.Channel.ExtraInstance.BindingChannelID]);
                    if (String.IsNullOrEmpty(bindingChannelID))
                        changes[Alias.Channel.ExtraInstance.BindingChannelID] = DBNull.Value;
                    else
                        changes[Alias.Channel.ExtraInstance.BindingChannelID] = new Guid(bindingChannelID);
                }

                Dal.Modify(id, changes);

                Dal.EndWork();
                Manager.UpdateExtraInstances();
            }
			public void Delete(int id)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Delete", guid);
                }

                Dal.Delete(id);

                Dal.EndWork();
                Manager.UpdateExtraInstances();
            }
			public DataTable List(int page, int pageSize, string condition, string sort)
            {
                if (CheckRight)
                {
                    ValidateRight("List", Guid.Empty);
                }

                return Dal.List(page, pageSize, condition, sort);
            }
			public int GetRecordCount(string condition)
            {
                return Dal.GetRecordCount(condition);
            }

            #region Properties
			private Dal.Channel.IExtraInstance Dal
            {
                get { return (Dal.Channel.IExtraInstance)DalBase; }
            }
            #endregion
        }
    }
}
